package ldkxingzhe.l365calendar.fragment.presenter;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

import ldkxingzhe.l365calendar.App;
import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.fragment.ScheduleAlarmFragment;
import ldkxingzhe.l365calendar.fragment.ScheduleAlertSetupFragment;
import ldkxingzhe.l365calendar.model.TimeStore;
import ldkxingzhe.l365calendar.utils.CalendarUtil;
import ldkxingzhe.l365calendar.utils.PreferenceHelper;

/**
 * 提醒界面的presenter
 * Created by ke on 16-1-31.
 */
public class ScheduleAlarmPresenter {
    @SuppressWarnings("unused")
    private static final String TAG = "ScheduleAlarmPresenter";

    private long startTime;
    private boolean isAllDay;
    private ArrayList<Integer> selectPosition;
    private String[] namesArray;
    private String[] timeArray;
    private long allDayAlarmOffsetTime;

    public long getAllDayAlarmOffsetTime() {
        return allDayAlarmOffsetTime;
    }

    public void onCreate(Bundle bundle){
        isAllDay = bundle.getBoolean(ScheduleAlarmFragment.IS_ALL_DAY, false);
        startTime = bundle.getLong(ScheduleAlarmFragment.START_TIME, -1);
        if(startTime == -1){
            Log.e(TAG, "startTime was not passed");
            startTime = new Date().getTime();
        }
        selectPosition = bundle.getIntegerArrayList(ScheduleAlarmFragment.POSITION_ARRAY);
        if(selectPosition == null) {
            selectPosition = new ArrayList<>();
            selectPosition.add(getCount() - 1);
        }
        if(isAllDay){
            namesArray = App.applicationContext.getResources().getStringArray(R.array.all_day_times);
            allDayAlarmOffsetTime = new PreferenceHelper(App.applicationContext).getLong(ScheduleAlarmFragment.ALL_DAY_ALARM_TIME_OFFSET);
        }else{
            namesArray = App.applicationContext.getResources().getStringArray(R.array.alarms_times);
            allDayAlarmOffsetTime = 0;
        }
        timeArray = new String[namesArray.length];
    }

    public boolean getIsAllDay() {
        return isAllDay;
    }

    public String getItemName(int index){
        return namesArray[index];
    }
    public boolean isSelected(int index){
        return selectPosition.contains(index);
    }
    public void onItemClick(int index){
        if(index == getCount() - 1){
            selectPosition.clear();
        }else{
            selectPosition.remove((Integer)(getCount() - 1));
        }
        if(isSelected(index)){
            selectPosition.remove((Integer)index);
        }else{
            selectPosition.add(index);
        }
        if(selectPosition.size() == 0){
            selectPosition.add(getCount() - 1);
        }
        Collections.sort(selectPosition);
        mView.notifyDateSetChanged();
        resetAlarmTime();
    }

    private void resetAlarmTime() {
        mView.alarmDescLayoutClear();
        for(Integer position : selectPosition){
            if(position == getCount() - 1){
                mView.alarmDescLayoutAddView(namesArray[position], null);
                break;
            }
            if(timeArray[position] == null){
                timeArray[position] = CalendarUtil.formatTime(alarmTime(position),
                        CalendarUtil.TIME_HM);
            }
            mView.alarmDescLayoutAddView(namesArray[position],
                    timeArray[position]);
        }
    }

    private long alarmTime(int index){
        long resultTime;
        if(isAllDay){
            resultTime = TimeStore.allDayTime[index];
        }else{
            resultTime = TimeStore.alarmTime[index];
        }
        return startTime - resultTime + allDayAlarmOffsetTime;
    }

    public int getCount(){
        if(getIsAllDay()){
            return TimeStore.allDayTime.length;
        }else{
            return TimeStore.alarmTime.length;
        }
    }

    public ArrayList<Integer> getSelectPosition() {
        Collections.sort(selectPosition);
        return selectPosition;
    }

    public void onCreateView() {
        resetAlarmTime();
        resetHitText();
    }

    private void resetHitText() {
        if(isAllDay){
            mView.setHintVisibility(View.VISIBLE);
            mView.setHintText(App.applicationContext.getResources().getString(R.string.all_day_alarm_time)
                    + ScheduleAlertSetupFragment.formatHM((int) allDayAlarmOffsetTime / 1000 / 60 / 60, (int) allDayAlarmOffsetTime / 1000 / 60 % 60));
        }else{
            mView.setHintVisibility(View.GONE);
        }
    }

    public void onAllDayAlertTimeChanged(int resultCode, Intent data) {
        allDayAlarmOffsetTime = data.getLongExtra(ScheduleAlertSetupFragment.TIME_LONG, 0);
        new PreferenceHelper(App.applicationContext)
                .getEditor()
                .putLong(ScheduleAlarmFragment.ALL_DAY_ALARM_TIME_OFFSET, allDayAlarmOffsetTime)
                .commit();
        resetHitText();
    }

    public interface PView{
        void notifyDateSetChanged();
        void alarmDescLayoutAddView(String name, String value);
        void alarmDescLayoutClear();
        void setHintText(String text);
        void setHintVisibility(int visibility);
    }
    private PView mView;
    public void setView(PView view) {
        this.mView = view;
    }
}
